package com.sojson.project.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.constant.Constant;
import com.sojson.enums.EAddrIsDefault;
import com.sojson.project.controller.param.verify.AddressesParamVerify;
import com.sojson.project.entity.po.Addresses;
import com.sojson.project.service.AddressesService;
import com.sojson.result.Result;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 收货地址表控制器
 * 
 * @author liu
 * @date 2021-03-19
 */
@Api(tags = "收货地址表管理")
@RestController
@RequestMapping(value = "/addresses")
public class AddressesController {

    @Resource
    private AddressesService addressesServiceImpl;

    /**
     * 单条插入
     * 
     * @param addresses
     * @return
     * @throws Exception 
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.login()")
    @ApiOperation("添加收货地址")
    public Object insert(@RequestBody Addresses addresses) throws Exception {
        // 校验参数
        Result<?> paramResult = AddressesParamVerify.insert(addresses);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        addresses.insertInit();
        addressesServiceImpl.insert(addresses);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    // @PostMapping("/insertList")
    public Object insertList(@RequestBody Addresses addresses) throws SQLException {
        return Result.success(addressesServiceImpl.insertList(addresses));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByDto")
    public Object deleteByDto(@RequestBody Addresses addresses) throws SQLException {
        return Result.success(addressesServiceImpl.deleteByDto(addresses));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/countAll")
    public Object countAll() throws SQLException {
        int countAll = addressesServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findAll")
    public Object findAll() throws SQLException {
        List<Addresses> findAll = addressesServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    // @PostMapping("/countByDto")
    public Object countByDto(@RequestBody Addresses addresses) throws SQLException {
        int countByDto = addressesServiceImpl.countByDto(addresses);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findByDto")
    @PreAuthorize("@ss.hasPermi('/addresses/findByDto')")
    @ApiOperation(value = "根据对象查询收货地址", response = Addresses.class)
    public Object findByDto(Addresses addresses) throws SQLException {
        List<Addresses> findByDto = addressesServiceImpl.findByDto(addresses);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteById")
    public Object deleteById(Integer id) throws SQLException {
        addressesServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @DeleteMapping(value = "/deleteByIdList/{ids}")
    @PreAuthorize("@ss.login()")
    @ApiOperation("通过id列表删除收货地址(物理删除)")
    public Object deleteByIdList(@PathVariable ArrayList<Integer> ids) throws SQLException {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(addressesServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateById")
    public Object updateById(@RequestBody Addresses addresses) throws SQLException {
        addressesServiceImpl.updateById(addresses);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody Addresses addresses) throws SQLException {
        return Result.success(addressesServiceImpl.updateStaticByIdList(addresses));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody Addresses addresses) throws SQLException {
        return Result.success(addressesServiceImpl.updateChangeByIdList(addresses));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param addresses
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/updatePartialById")
    @PreAuthorize("@ss.login()")
    @ApiOperation("通过id更新收货地址(局部更新)")
    public Object updatePartialById(@RequestBody Addresses addresses) throws Exception {
        // 校验参数
        Result<?> paramResult = AddressesParamVerify.updatePartialById(addresses);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        addresses.updateInit();
        addresses.setUpdateNull();
        addressesServiceImpl.updatePartialById(addresses);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody Addresses addresses) throws SQLException {
        return Result.success(addressesServiceImpl.updateStaticPartialByIdList(addresses));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody Addresses addresses) throws SQLException {
        return Result.success(addressesServiceImpl.updateChangePartialByIdList(addresses));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping("/findById")
    public Object findById(Integer id) throws SQLException {
        // 校验参数
        Result<?> paramResult = AddressesParamVerify.findById(id);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Addresses findById = addressesServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<Addresses> findByIdList = addressesServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param addresses
     * @return
     * @throws SQLException
     */
    // @PostMapping("/findIdByDto")
    public Object findIdByDto(@RequestBody Addresses addresses) throws SQLException {
        ArrayList<Integer> findIdByDto = addressesServiceImpl.findIdByDto(addresses);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @PostMapping("/clear")
    public Object clear() throws SQLException {
        addressesServiceImpl.clear();
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param addresses
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/findAppByDto")
    @PreAuthorize("@ss.login()")
    @ApiOperation(value = "根据对象查询收货地址", response = Addresses.class)
    public Object findAppByDto(Addresses addresses) throws SQLException, IOException {
        addresses.setMId(TokenUtil.getUserId());
        List<Addresses> findByDto = addressesServiceImpl.findByDto(addresses);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 获取商家默认地址
     * 
     * @param addresses
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/findMerchantDefault")
    @ApiOperation(value = "获取商家默认地址", response = Addresses.class)
    public Object findMerchantDefault(Addresses addresses) throws SQLException, IOException {
        addresses.setMId(Constant.ADMIN_ID);
        addresses.setIsDefault(EAddrIsDefault.YES.getCode());
        List<Addresses> findByDto = addressesServiceImpl.findByDto(addresses);
        if (findByDto.size() > 0) {
            return Result.success(findByDto.get(0));
        }
        return Result.fail("商家未设置默认地址");
    }

}